/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-09-26 18:56:59
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-12-29 14:49:20
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import { MessageService } from '@abp/message/message.service';
import { BomContentServiceProxy, BomEditList, MoadifManageServiceProxy, ApproveInput } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { FileDownloadService } from '@shared/utils/file-download.service';
import { LazyLoadEvent, TreeNode } from 'primeng/api';
@Component({
    selector: 'app-bom',
    templateUrl: './bom.component.html',
    styleUrls: ['./bom.component.less']
})
export class BomComponent extends AppComponentBase {
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() editData: any;  //行数据
    @Input() title: string;  //标题
    @Input() flag: boolean;  //查看还是审批
    saving = false;
    active = false;
    loadingTable = false;
    treeData: any = [];
    approvalForm = new ApproveInput();
    approvalList = [];
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _bomContentServiceProxy: BomContentServiceProxy,
        private _moadifManageServiceProxy: MoadifManageServiceProxy,
        private _fileDownloadService: FileDownloadService,
    ) {
        super(injector);
    }

    ngOnInit() {
    }
    selectStatus(value) {
        switch (value) {
            case 0:
                return '编制中';
            case 1:
                return '审核中';
            case 2:
                return '被驳回';
            case 3:
                return '已发布';
            case 4:
                return '已维护';
            case 5:
                return '待维护';
            default:
                return null;
        }
    }
    //保存任务
    save(): void {
        //     this._bomContentServiceProxy.addOrUpBomList(input)
        //         .pipe(finalize(() => { this.saving = false; }))
        //         .subscribe(() => {
        //             this._messageService.success('保存成功');
        //             this.close();
        //             this.modalSave.emit();
        //         });
    }
    //打开弹窗
    onShown(): void {

    }
    accept() {
        //审批同意1
        abp.ui.setBusy();
        this.approvalForm.bappId = this.editData.bAppId;
        this.approvalForm.operation = 1;
        this._bomContentServiceProxy.approvebomModif(this.approvalForm)
            .pipe(finalize(() => { }))
            .subscribe(() => {
                abp.ui.clearBusy();
                this._messageService.success('操作成功');
                this.modalSave.emit();
                this.close();
            });
    }
    confues() {
        //审批拒绝0
        abp.ui.setBusy();
        this.approvalForm.bappId = this.editData.bAppId;
        this.approvalForm.operation = 0;
        this._bomContentServiceProxy.approvebomModif(this.approvalForm)
            .pipe(finalize(() => { }))
            .subscribe(() => {
                abp.ui.clearBusy();
                this._messageService.success('操作成功');
                this.modalSave.emit();
                this.close();
            });
    }
    //查看新增任务
    show(): void {
        this.initEditInfo();
        this.active = true;
        this.modal.show();
    }
    //初始化修改数据
    initEditInfo() {
        this.getTreedata();
    }
    getClass(data) {
        if (data.result == 0) {
            return 'approval-info';
        } else if (data.result == 1) {
            return 'approval-success';
        } else if (data.result == 2) {
            return 'approval-error';
        }

    }
    getTreedata() {
        this.loadingTable = true;
        this._bomContentServiceProxy.displayBomBusinessById(
            this.editData.bAppId
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.approvalList = result.workAuditorList;
                this.loadingTable = false;
                this.treeData = [];
                this.treeData.push(result.bomDetaillists);
                this.expandAll();

            });
    }
    expandAll() {
        this.treeData[0].expanded = true;
        for (const node of this.treeData[0].children) {
            this.TreeExpandRecursive(node, true);
        }
    }
    private TreeExpandRecursive(node: TreeNode, isExpand: boolean) {
        node.expanded = isExpand;
        if (node.children) {
            node.children.forEach((childNode) => {
                this.TreeExpandRecursive(childNode, isExpand);
            });
        }
    }
    //关闭事件
    close(): void {
        //清空form表单
        this.treeData = [];
        this.approvalForm = new ApproveInput();
        this.modal.hide();
    }

}
